package com.example.smarttarfficmobile192_08;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    String pathGet = "http://28600048s7.imdo.co/transportservice/action/GetRoadStatus";
    private WebView wbRoadNew;
    private static final long DELAAY=5*1000;
    OkHttpClient okHttpClient = new OkHttpClient();
    Call call;
    Request request;
    int[] Status = new int[7];
    private TextView tvTitle;
    private ImageView imgJianTou;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        final Intent intent=new Intent(this,MainActivity.class);
        Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
              startActivity(intent);
            }
        };
        timer.schedule(task,DELAAY);
        getRoadByOkHttp(1);getRoadByOkHttp(2);
        getRoadByOkHttp(3);getRoadByOkHttp(4);
        getRoadByOkHttp(5);getRoadByOkHttp(6);getRoadByOkHttp(7);
        wbRoadNew.loadUrl("file:////android_asset/echarts.html");
        wbRoadNew.getSettings().setJavaScriptEnabled(true);
        wbRoadNew.addJavascriptInterface(new JsInterface(), "obj");
    }

    private void getRoadByOkHttp(final int roadId) {
        MediaType mediaType = MediaType.parse("application;charset=utf-8");
        JsonObject jsonObject = new JsonObject();
        int i=1;
        if (roadId<=1){
            i=1;
        }else if (roadId<=2){
            i=2;
        }else if (roadId<=3){
            i=3;
        }else if (roadId<=4){
            i=4;
        }else if (roadId<=5){
            i=5;
        }else if (roadId<=6){
            i=6;
        }else if (roadId<=7){
            i=7;
        }
        jsonObject.addProperty("UserName","user"+i);
        jsonObject.addProperty("RoadId", roadId);
        String json = jsonObject.toString();
        Log.i("json", json);
        RequestBody requestBody = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(pathGet)
                .post(requestBody)
                .build();
        call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String json = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        RoadBean roadBean = gson.fromJson(json, RoadBean.class);
                        Status[0] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[1] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[2] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[3] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[4] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[5] = Integer.parseInt(String.valueOf(roadBean.getStatus()));
                        Status[6] = Integer.parseInt(String.valueOf(roadBean.getStatus()));


                    }
                });
                Log.i("json", json);
            }
        });
    }

    class JsInterface {
        @JavascriptInterface
        public String jsonData() {
            JsonArray jsonArray = new JsonArray();
            jsonArray.add(Status[0]);
            jsonArray.add(Status[1]);
            jsonArray.add(Status[2]);
            jsonArray.add(Status[3]);
            jsonArray.add(Status[4]);
            jsonArray.add(Status[5]);
            jsonArray.add(Status[6]);
            Gson gson = new Gson();
            String json = gson.toJson(jsonArray);
            return json;
        }

        }
    private void initView() {
        wbRoadNew = (WebView) findViewById(R.id.wbRoadNew);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        imgJianTou = (ImageView) findViewById(R.id.imgJianTou);
    }
}